// This File Compares The Shape of the Model Implied Price Path to the Actual Price Path
// Estimating Non-Linear Version of the Model
clear all	
// Loading Data
use ../dta/model_estimation_data.dta, clear
tab date2, gen(date2_dum)
tset code date2

// Non-Linear Estimation - Excess Price Changes - Base
nl exp2: excess_price_diff t if  estimation_sample==1 & vintage!=0 & vintage!=1  , variables(excess_price_diff t) 
disp "theta"
scalar theta=_b[/b2]
scalar delta_1=_b[/b1]
disp theta 
disp delta_1

// Plotting Model Estimates Against 	
clear
set obs 1000
gen t = _n -950
gen theta_t=theta^t*(t<=0)
gen sum_theta_t=sum(theta_t)
gen delta_sum_theta_t=delta_1*sum_theta_t
tempfile tsim
save `tsim'

// Loading Data
use ../dta/model_estimation_data.dta, clear
replace price=price590
reghdfe price, a(date code) resid
predict price_resid, r	
drop t
gen t=date2
replace t=t-636 if vintage==2			 
replace t=t-644 if vintage==3
replace t=t-656 if vintage==4
keep if vintage==2|vintage==3|vintage==4 
collapse price price_resid, by(t)	
twoway  (line price_resid t if t<=0 & t>=-20, xline(0))	

local l_18 "-18(6)0"
local l_20 "-20(5)0"
local l_24 "-24(6)0"	
local l_36 "-36(12)0"		
local l_48 "-48(12)0"			

merge 1:1 t using `tsim', keep(match)
keep if t<=0 & t>=-48
foreach var in delta_sum_theta_t price_resid{
	sum `var'
	replace `var'=`var'-r(mean)
}

// Make Plots
twoway  (line price_resid t , lcolor(black) lwidth(thick) )	///
		(line delta_sum_theta_t t, lcolor(blue%60) lwidth(thick)), xline(0, lwidth(thick))	///
		xtitle(Period) xlabel(-48(12)0)  ytitle("De-meaned Price") xscale(range(-48 6)) ///
		legend(on order(1 "Data" 2 "Model Estimates") region(lcolor(white)) position(6) row(1) ) 
graph export ../gph/model_fit.pdf, replace

		
